
public class ConvexSequence {
	public long getMinimum(int[] a) {
		int n = a.length;
		long[] aa = new long[n];
		for (int i=0; i<n; i++) {
			aa[i] = a[i];
//			System.out.print(aa[i] + " ");
		}
//		System.out.println();
		
		long res = 0;

		
		for (int l=0; l<n; l++) {
			for (int h=l+2; h<n; h++) {				
				for (int i=l+1; i<=h-1; i++) {
					long t = (long)Math.floor(1e-10 + aa[l] + (aa[h]-aa[l])*(i-l)/(double)(h-l));
					if (aa[i] > t) {
						res += aa[i] - t;
						aa[i] = t;
					}
				}
			}
		}
		

		return res;
	}
	
	public static void main(String[] args) {
		long res = new ConvexSequence().getMinimum(new int[]{6,5,1,5,3,3});
		System.out.println(res);
	}
}
